<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <!-- <el-form-item label="日期" style="width: 350px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item> -->
        <el-form-item label="生产单编号" prop="produceNo">
          <el-input
            v-model.trim="queryParams.produceNo"
            style="width: 180px"
            placeholder="请输入生产单编号"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车间" prop="workshopName">
          <el-select
            style="width: 180px"
            @change="handleQuery"
            v-model="queryParams.workshopName"
            placeholder="请选择"
          >
            <el-option
              v-for="item in ems_workshop"
              :key="item.value"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model.trim="queryParams.productName"
            style="width: 180px"
            placeholder="请输入产品名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
        </el-col> -->
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        height="65vh"
        :data="dataList"
        row-key="produceId"
        @expand-change="handleHeaderClick"
      >
        <el-table-column type="expand">
          <template #default="props">
            <div>
              <!-- <p m="t-0 b-2">State: {{ props.row.state }}</p>
          <p m="t-0 b-2">City: {{ props.row.city }}</p>
          <p m="t-0 b-2">Address: {{ props.row.address }}</p>
          <p m="t-0 b-2">Zip: {{ props.row.zip }}</p>
          <h3>Family</h3> -->
              <el-table :data="props.row.bindingMode" border>
                <!-- <el-table-column
                  width="60"
                  label="序号"
                  type="index"
                  align="center"
                /> -->
                <el-table-column
                  align="center"
                  label="生产单编号"
                  width="160"
                  prop="produceNo"
                />
                <el-table-column
                  align="center"
                  label="车间"
                  prop="workshopName"
                />
                <el-table-column
                  align="center"
                  label="交货日期"
                  prop="deliverDate"
                >
                  <template v-slot="{ row }">
                    {{ row.deliverDate && row.deliverDate.split("T")[0] }}
                  </template>
                </el-table-column>
                <el-table-column align="center" label="品种" prop="breedType" />
                <el-table-column
                  align="center"
                  label="组成"
                  prop="composition"
                />
                <el-table-column align="center" label="印张" prop="sheet" />
                <el-table-column
                  align="center"
                  label="版材尺寸"
                  prop="sheetSize"
                >
                  <template #default="scope">
                    <dict-tag
                      :options="ems_machine_info_size"
                      :value="scope.row.sheetSize"
                    />
                  </template>
                </el-table-column>
                <el-table-column
                  align="center"
                  label="订单数量"
                  prop="orderCount"
                />
                <el-table-column
                  align="center"
                  label="装订方式"
                  prop="bindingMode"
                >
                  <template #default="scope">
                    <dict-tag
                      :options="ems_order_binding_mode"
                      :value="scope.row.bindingMode"
                    />
                  </template>
                </el-table-column>
                <el-table-column align="center" label="印刷状态" prop="ysDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.ysDate == '已完成' ||
                        scope.row.ysDate == '印刷中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleView(scope.row, '1')"
                        text
                      >
                        {{ scope.row.ysDate }}
                      </el-button>
                    </span>
                    <span
                      style="color: red"
                      v-else-if="scope.row.ysDate == '--'"
                    >
                      {{ scope.row.ysDate }}
                    </span>
                    <span v-else>
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '2')"
                        text
                      >
                        {{ scope.row.ysDate }}
                      </el-button>
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="覆膜状态" prop="fmDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.fmDate == '已完成' ||
                        scope.row.fmDate == '覆膜中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleView(scope.row, '4')"
                        text
                      >
                        {{ scope.row.fmDate }}
                      </el-button>
                    </span>
                    <span v-else-if="scope.row.fmDate == '已排产'">
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '7')"
                        text
                      >
                        {{ scope.row.fmDate }}
                      </el-button>
                    </span>
                    <span style="color: red" v-else>
                      {{ scope.row.fmDate }}</span
                    >
                  </template>
                </el-table-column>
                <el-table-column align="center" label="裁切状态" prop="cqDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.cqDate == '已完成' ||
                        scope.row.cqDate == '裁切中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleView(scope.row, '3')"
                        text
                      >
                        {{ scope.row.cqDate }}
                      </el-button>
                    </span>
                    <span v-else-if="scope.row.cqDate == '已排产'">
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '5,6')"
                        text
                      >
                        {{ scope.row.cqDate }}
                      </el-button>
                    </span>
                    <span style="color: red" v-else>
                      {{ scope.row.cqDate }}</span
                    >
                  </template>
                </el-table-column>
                <el-table-column align="center" label="折页状态" prop="zyDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.zyDate == '已完成' ||
                        scope.row.zyDate == '折页中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleView(scope.row, '2')"
                        text
                      >
                        {{ scope.row.zyDate }}
                      </el-button>
                    </span>
                    <span v-else-if="scope.row.zyDate == '已排产'">
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '4')"
                        text
                      >
                        {{ scope.row.zyDate }}
                      </el-button>
                    </span>
                    <span style="color: red" v-else>
                      {{ scope.row.zyDate }}
                    </span>
                  </template>
                </el-table-column>
                <el-table-column align="center" label="装订状态" prop="zdDate">
                  <template #default="scope">
                    <span
                      style="color: blue"
                      v-if="
                        scope.row.zdDate == '已完成' ||
                        scope.row.zdDate == '装订中'
                      "
                    >
                      <el-button
                        type="primary"
                        @click="handleView(scope.row, '5')"
                        text
                      >
                        {{ scope.row.zdDate }}
                      </el-button>
                    </span>
                    <span v-else-if="scope.row.zdDate == '已排产'">
                      <el-button
                        type="primary"
                        @click="handleViewLoading(scope.row, '3')"
                        text
                      >
                        {{ scope.row.zdDate }}
                      </el-button>
                    </span>
                    <span style="color: red" v-else>
                      {{ scope.row.zdDate }}</span
                    >
                  </template>
                </el-table-column>
                <el-table-column align="center" label="发货状态" prop="fhDate">
                  <template #default="scope">
                    <el-button
                      type="primary"
                      @click="handleViewCount(scope.row, '3')"
                      text
                    >
                      {{ scope.row.fhDate }}
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </template>
        </el-table-column>
        <el-table-column width="60" label="序号" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <dict-tag :options="ems_order_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="数据状态"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="isSuspend"
          v-if="columns[0].visible"
        >
          <template v-slot="{ row }">
            <el-tag
              :type="row.isSuspend == 1 ? 'error' : 'success'"
              effect="dark"
              round
            >
              {{ row.isSuspend == 1 ? "暂停" : "正常" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="生产单编号" min-width="120" prop="produceNo" />
        <el-table-column
          align="center"
          width="100"
          label="车间"
          prop="workshopName"
        />
        <el-table-column
          align="center"
          min-width="240"
          label="产品名称"
          prop="productName"
        />
        <el-table-column align="center" label="订单数量" prop="orderCount" />
        
        <el-table-column align="center" label="加急交货日期"  min-width="150" prop="urgentDate">
          <template v-slot="{ row }">
            <span style="color: red">
              {{ parseTime(row.urgentDate) }}
            </span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="下单时间" min-width="100" prop="submitDate">
          <template v-slot="{ row }">
            {{ row.submitDate?.split("T")[0] ?? "" }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="印张集合" prop="sheetList" />
        <el-table-column
          label="操作"
          align="center"
          width="100"
          fixed="right"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              link
              type="primary"
              icon="Printer"
              @click="handlePrint(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- <el-table v-loading="loading" border :data="dataList">
                <el-table-column label="序号" width="60" type="index" align="center" />
                <el-table-column
                    label="状态"
                    min-width="100"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="status"
                    v-if="columns[0].visible"
                >
                    <template v-slot="{ row }">
                        <el-tag :type="row.status == 1 ? 'danger' : ''" effect="dark">
                            {{ row.status == 1 ? '停用' : '启用' }}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    label="单据类型"
                    min-width="160"
                    align="center"
                    prop="machineName"
                    v-if="columns[3].visible"
                ></el-table-column>
                <el-table-column
                    label="报工单号"
                    min-width="160"
                    align="center"
                    prop="machineName"
                    v-if="columns[3].visible"
                ></el-table-column>
                <el-table-column
                    label="机台"
                    min-width="160"
                    align="center"
                    prop="machineName"
                    v-if="columns[3].visible"
                ></el-table-column>
                <el-table-column
                    label="生产单编码"
                    v-if="columns[8].visible"
                    align="center"
                    width="120"
                    prop="produceNo"
                ></el-table-column>
                <el-table-column
                    label="印刷序列号"
                    min-width="160"
                    v-if="columns[10].visible"
                    prop="producePieceNo"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="贴标"
                    width="120"
                    v-if="columns[11].visible"
                    prop="pieceview"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="已分配任务量"
                    v-if="columns[5].visible"
                    width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="printAmount"
                ></el-table-column>
                <el-table-column
                    label="已完成任务量"
                    v-if="columns[6].visible"
                    width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="total"
                ></el-table-column>
                <el-table-column
                    label="未完成量"
                    v-if="columns[7].visible"
                    width="120"
                    align="wtotal"
                    :show-overflow-tooltip="true"
                    prop="bookName"
                ></el-table-column>

                <el-table-column
                    label="预计用纸"
                    width="120"
                    v-if="columns[12].visible"
                    prop="expectTime"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="实际用纸"
                    width="120"
                    v-if="columns[12].visible"
                    prop="expectTime"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="用纸盈亏"
                    width="120"
                    v-if="columns[12].visible"
                    prop="expectTime"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="当前库存"
                    width="120"
                    v-if="columns[12].visible"
                    prop="expectTime"
                    align="center"
                ></el-table-column>
            </el-table> -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <printView
        :visible="printOpen"
        :printList="printList"
        @cancel="
          () => {
            printOpen = false;
            printList = [];
          }
        "
      ></printView>
    </div>
  </div>
</template>
<script setup name="BIndWaitScheduling">
import { getMachineTask } from "@/api/production/cover.js";
import {
  waitBindingpc,
  getscheduledetail,
} from "@/api/production/bIndWaitScheduling.js";
import printView from "@/components/PrintView";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const printOpen = ref(false);
const printList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const sheetTotal = ref(0);
const platemakingTotal = ref(0);
const isShowProduct = ref(false);
const updateTextId = ref("");
// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `新/旧版`, visible: true },
  { key: 2, label: `制版日期`, visible: true },
  { key: 3, label: `制版编码`, visible: true },
  { key: 4, label: `生产单编码`, visible: true },
  { key: 5, label: `出版编码`, visible: true },
  { key: 6, label: `书刊编码`, visible: true },
  { key: 7, label: `书刊名称`, visible: true },
  { key: 8, label: `印刷序列号`, visible: true },
  { key: 9, label: `品种`, visible: true },
  { key: 10, label: `组成`, visible: true },
  { key: 11, label: `印张`, visible: true },
  { key: 12, label: `印数`, visible: true },
  { key: 13, label: `印色`, visible: true },
  { key: 14, label: `版材数量`, visible: true },
  { key: 15, label: `制版数量`, visible: true },
  { key: 16, label: `版材尺寸`, visible: true },
  { key: 17, label: `制版人`, visible: true },
  { key: 18, label: `烤版人`, visible: true },
  { key: 19, label: `完成日期`, visible: true },
  { key: 20, label: `取版人`, visible: true },
  { key: 21, label: `取版日期`, visible: true },
  { key: 22, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    // isJoinFm: "0",
  },
});
const handleHeaderClick = (row, expanded) => {
  getscheduledetail({ produceId: row.produceId, bookCode: row.bookCode }).then(
    (res) => {
      row.bindingMode = res.rows;
    }
  );
};
const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const {
  ems_workshop,
  ems_machine_info_size,
  ems_order_binding_mode,
  ems_machine_info_type,
  ems_machine_info_model,
  ems_order_status,
} = proxy.useDict(
  "ems_workshop",
  "ems_machine_info_type",
  "ems_order_status",
  "ems_machine_info_model",
  "ems_machine_info_size",
  "ems_order_binding_mode"
);

const getList = () => {
  loading.value = true;
  // queryParams.value.startDate = Array.isArray(dateRange.value)
  //   ? dateRange.value[0]
  //   : undefined;
  // queryParams.value.endDate =
  //   Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  waitBindingpc(queryParams.value)
    .then((res) => {
      dataList.value = res.rows;
      loading.value = false;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
  // getMachineTask({
  //     ...queryParams.value,
  //     startDate: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
  //     endDate: Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
  // })
  //     .then(res => {
  //         loading.value = false
  //         dataList.value = res.rows
  //         if (res.rows.length > 0) {
  //             sheetTotal.value = res.rows[0].sheetTotal
  //             platemakingTotal.value = res.rows[0].platemakingTotal
  //         }
  //         total.value = res.total
  //     })
  //     .catch(err => {
  //         loading.value = false
  //     })
};
//  关联生产单
const handleContactCover = (row) => {
  isShowProduct.value = true;
  updateTextId.value = row.produceId;
};
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "sheetAmount":
        sums[index] = sheetTotal.value;
        break;
      case "platemakingNumber":
        sums[index] = platemakingTotal.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

const handleView = (row, type) => {
  router.push({
    path: "/Productionmanagementdistribution/production/searchReportDetail",
    query: {
      breed: row.breedType,
      type: type,
      produceNo: row.produceNo,
      status: -2,
      info: true,
    },
  });
};

// 折页中 印刷中 覆膜中 裁切中
const handleViewLoading = (row, type) => {
  router.push({
    path: "/production/orderProgress/processPenetration",
    query: {
      breedType: row.breedType,
      machineType: type,
      produceNo: row.produceNo,
      info: true,
    },
  });
};

//发货数量
const handleViewCount = (row, type) => {
  router.push({
    path: "/production/orderProgress/quantityPenetration",
    query: {
      produceNo: row.produceNo,
      info: true,
    },
  });
};

const handlePrint = (row) => {
  printList.value = [row];
  printOpen.value = true;
};
getList();
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
.line {
  display: inline-block;
  height: 10px;
  width: 100%;
  background-color: red;
}
</style>
